def exec_try():
    try:
        print('code started')
        raise KeyError
        return 1
    except KeyError as e:
        print('key error')
        return 2
    else:
        print('other error')
        return 3
    finally:
        print('finally')
        return 4


class Sample:
    def __enter__(self):
        print("enter")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print("exit")
        print(exc_type)
        print(exc_val)
        print(exc_tb)

    def do_something(self):
        print("doing something")


if __name__ == "__main__":
    result = exec_try()
    print(result)
    with Sample() as sample:
        sample.do_something()
